package cn.yogehaoren.message_properties;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @author WangNing
 * @since 1.0
 */
public class MessagePropertiesProducer {

    public static void main(String[] args) throws IOException, TimeoutException {

        //1. 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("k8s-node2");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2. 通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        // 3. 通过Connection创建Channel
        Channel channel = connection.createChannel();

        Map<String, Object> customerHeader = new HashMap<>();
        customerHeader.put("my1", "111");
        customerHeader.put("my2", "222");

        AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                .deliveryMode(2) //1- 代表持久化 2-代表不持久化
                .expiration("10000") // 消息的过期时间
                .contentEncoding("UTF-8") // 消息的编码
                .headers(customerHeader)
                .build();


        for(int i=0;i<10;i++){
            // 4. 通过Channel发送数据
            channel.basicPublish("", "test001", properties, ("Hello RabbitMQ-"+i).getBytes());
        }

        // 5. 关闭连接
        channel.close();
        connection.close();
    }

}
